package com.luxx.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.luxx.config.Constant;
import com.luxx.util.HttpUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by luxx on 2018/3/22 0022.
 */
@Service
public class WxService {

    public static final String TOKEN_API = "https://api.weixin.qq.com/cgi-bin/token";
    public static final String MENU_API = "https://api.weixin.qq.com/cgi-bin/menu/create";
    public static final String TOKEN_GRANT_TYPE = "client_credential";

    /**
     * 获取access_token
     *
     * @return
     * @throws IOException
     */

    public static String getToken() throws IOException {
        Map<String, String> params = new HashMap<>(16);
        params.put("grant_type", TOKEN_GRANT_TYPE);
        params.put("appid", Constant.WX_APPID);
        params.put("secret", Constant.WX_SECRET);
        HttpGet get = new HttpGet(TOKEN_API + "?" + HttpUtil.createFormString(params));
        CloseableHttpResponse response = HttpClientBuilder.create().build().execute(get);
        JSONObject json = JSONObject.parseObject(EntityUtils.toString(response.getEntity()).trim());

        String access_token = json.getString("access_token");
        if (access_token != null) {
            return access_token;
        } else {
            //todo LOG
            String errcode = json.getString("errcode");
            return null;
        }
    }

    /**
     * 创建自定义菜单
     * 该接口未被授权
     */
    public void setMenu() throws IOException {
        JSONObject json = new JSONObject();
        JSONArray button = new JSONArray();
        json.put("button", button);
        JSONObject button1 = new JSONObject();
        button.add(button1);
        button1.put("name", "博客");
        button1.put("type", "view");
        button1.put("url", "www.luxiaoxiang.xyz");

        HttpPost post = new HttpPost(MENU_API + "?access_token=" + getToken());
        post.setEntity(new StringEntity(json.toJSONString()));
        CloseableHttpResponse response = HttpClientBuilder.create().build().execute(post);
        JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity()).trim());
    }

    public static void main(String[] args) throws IOException {
        //System.out.println(getToken());
        JSONObject json = new JSONObject();
        JSONArray button = new JSONArray();
        json.put("button", button);
        JSONObject button1 = new JSONObject();
        button.add(button1);
        button1.put("name", "博客");
        button1.put("type", "view");
        button1.put("url", "www.luxiaoxiang.xyz");
        System.out.println(json);
    }
}
